<?php

/**
 * 用户的微官网模板配置logic
 */

namespace Member\Logic;

class MallUserTemlatesLogic extends CommonLogic {
	
	/**
	 * 获取模板详细
	 * @param  integer $tid 模板ID
	 * @return array       当前模型详细信息
	 */
	public function detail($map){
		//$map = array('tid'=>$tid,'uid'=>$uid);
		$data = $this->field(true)->where($map)->find();
		if(!$data){
			$this->error = '获取详细信息出错！';
			return false;
		}
		
		$data['configs'] = serializeMysql($data['configs'],1);
	
		return $data;
	}
    
	//获取全部的模板信息
	public function allLists($map = array(),$field = array('id','bg','name')){
	    $data = $this->where($map)->field($field)->order('listorder DESC')->select();
	    
	    if(in_array('configs', $field)){
	        foreach ($data as &$v){
	            $v['configs'] = serializeMysql($v['configs'],1);
	        }
	    }
	    
	    return $data;
	    
	}
	
	/**
	 * (non-PHPdoc)
	 * @see \Member\Logic\BaseLogic::update()
	 */
	public function update($data){
		
		if(empty($data)){
			$data = $this->create();
		}
		
		if($data === false){
			return false;
		}
		
		$data['configs'] = serializeMysql($data['configs']);
		
		$map = array('uid'=>$data['uid'],'tid'=>$data['tid'],'type'=>$data['type']);
		if($data['cid']!=null){
			$map['cid']= $data['cid'];
		}
		$info = $this->where($map)->find();
		/* 添加或更新数据 */
		if(empty($info['id'])){//新增数据
			unset($data['id']);
			$id = $this->add($data);
			if(!$id){
				$this->error = '新增详细内容失败！';
				return false;
			}
		} else { //更新数据
			unset($data['id']);
			$status = $this->where($map)->save($data);
			if(false === $status){
				$this->error = '更新详细内容失败！';
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see \Member\Logic\BaseLogic::autoSave()
	 */
	public function autoSave($id = 0){
		 
	}
}